<?php
require_once("includes/pagetop.php");

if(!isset($_SESSION["Allyfolio_User_ID"]) || $_SESSION["Allyfolio_User_ID"]=="")
{
	redirect("index.php");	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>AllyFolio | Blogs </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />
<script src="js/general.js"></script>
<script src="js/functions.js"></script>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>	
<script src="js/jquery1.10.2.min.js"></script>

<script src="js/jquery_raty.js"></script>
<script src="allyfolio_admin/Classes/ckeditor/ckeditor.js"></script>
<script src="js/tagsinput.js"></script>
<link rel="stylesheet" href="css/tagsinput.css" />
</head>

<body>

	<div id="main_wrapper">
<?php
    $pageheading="Blogs";
    require_once("includes/header.php");
    
	if(isset($_POST['blog'])&& count($_POST['blog'])>0)
	{
		$data= $_POST['blog'];
		
		//echo "<pre>";
//		print_r($data);
//		die();
//		
		if($data["blog_id"]!="" && $data["blog_id"]>0)
		{
			$qryUpdate="update af_blogs set blog_title=:blog_title,blog_description=:blog_description where user_id='".$_SESSION['Allyfolio_User_ID']."' and status=0 and blog_id=:blog_id  ";
			$pdodbobj->query($qryUpdate,array(":blog_title"=>$data["blog_title"],":blog_description"=>$data["blog_description"]));
			
			////Kuch kar image ka bhi
//			if(!file_exists(WEB_URL.'uploads/blog_images/'.$data["blog_image"]))
//			{
//				$source_path = 'uploads/temp_blog_images/'.$data["blog_image"];
//				$target_path = 'uploads/blog_images/'.$data["blog_image"];
//				copy($source_path,$target_path);
//			}
			
			$qryDelete="delete from af_blog_links where  blog_id='".$blogid."'";
			$pdodbobj->query($qryDelete,array());
			
			$linksArr=explode(",",$data["link_address"]);
			for($i=0;$i<count($linksArr);$i++)
			{
				if(trim($linksArr[$i])!="")
				{
					$blog_link=$linksArr[$i];
					if(!(strstr($linksArr[$i],"http://")) && !(strstr($linksArr[$i],"https://")))
				    {
				        $blog_link="http://".$linksArr[$i];
				    }
				    
				    $insertqry="insert into af_blog_links set blog_id='".$blogid."',blog_link=:blog_link";
	        		$pdodbobj->query($insertqry,array(":blog_link"=>$blog_link));
	        	}	
			}
		 	$msg_success="Blog has been added successfully.";
		 	?>
		 	<script>
		 		window.location.href="blog.php?msg_success=<?php echo $msg_success; ?>";
			 </script>	
			 <?php
		}	
		else
		{
			$condition=array(":user_id"=>$_SESSION['Allyfolio_User_ID']);
			$qryBlogPageInfo="select * from af_blog_page where user_id =:user_id ";
			$arrBlogPageInfo=$pdodbobj->fetch_array($pdodbobj->query($qryBlogPageInfo,$condition));
			if(count($arrBlogPageInfo)>0)
			{
				$blog_page_id=$arrBlogPageInfo[0]["blog_page_id"];
			}
			else
			{
				$qryInsert="insert into af_blog_page set blog_page_image='',  user_id =:user_id ";
				$pdodbobj->query($qryInsert,$condition);
				$blog_page_id=$pdodbobj->last_id();
			}
    			
			$insertqry="insert into af_blogs set user_id='".$_SESSION['Allyfolio_User_ID']."',blog_title=:blog_title,blog_description=:blog_description,blog_page_id=:blog_page_id,post_date='".time()."'";
	        $pdodbobj->query($insertqry,array(":blog_title"=>$data["blog_title"],":blog_description"=>$data["blog_description"],":blog_page_id"=>$blog_page_id));
	        
	        if($pdodbobj->affectedrows>0)
			{
				$blogid=$pdodbobj->last_id();
				
				//$source_path = 'uploads/temp_blog_images/'.$data["blog_image"];
//				$target_path = 'uploads/blog_images/'.$data["blog_image"];
//				copy($source_path,$target_path);
				
				$linksArr=explode(",",$data["link_address"]);
				for($i=0;$i<count($linksArr);$i++)
				{
					if(trim($linksArr[$i])!="")
					{
						$blog_link=$linksArr[$i];
						if(!(strstr($linksArr[$i],"http://")) && !(strstr($linksArr[$i],"https://")))
					    {
					        $blog_link="http://".$linksArr[$i];
					    }

					    $insertqry="insert into af_blog_links set blog_id='".$blogid."',blog_link=:blog_link";
		        		$pdodbobj->query($insertqry,array(":blog_link"=>$blog_link));
		        	}	
				}
			 	$msg_success="Blog has been added successfully.";
			 	?>
			 	<script>
			 		window.location.href="blog.php?msg_success=<?php echo $msg_success; ?>";
				 </script>	
			 	<?php
			}
			else
			{
				$msg_error="Looks like there is some problem. Please try after some time.";
			}	
		}	
	}
	else if(isset($_REQUEST["blog_id"]) && $_REQUEST["blog_id"] >0)
	{
		$condition=array(":blog_id"=>$_REQUEST["blog_id"]);
		$qryBlog="select * from af_blogs where user_id='".$_SESSION['Allyfolio_User_ID']."' and status=0 and blog_id=:blog_id order by post_date desc limit 1";
		$arrBlog=$pdodbobj->fetch_array($pdodbobj->query($qryBlog,$condition));
		if(count($arrBlog)>0)
		{
			$qryLinks="select * from  af_blog_links where blog_id='".$arrBlog[0]["blog_id"]."'";
			$arrBlogLinks=$pdodbobj->fetch_array($pdodbobj->query($qryLinks,array()));
			
			$strBlogs="";
			if(count($arrBlogLinks)>0)
			{
				for($i=0; $i<count($arrBlogLinks); $i++)
				{
					if(trim($arrBlogLinks[$i]["blog_link"])!="")
					{
						$strBlogs .=trim($arrBlogLinks[$i]["blog_link"]).",";
					}
				}
				if($strBlogs!="")
				{
					$strBlogs=substr($strBlogs,0,strlen($strBlogs)-1);
				}
			}
		
			$data= $arrBlog[0];
			$data["link_address"]=$strBlogs;
		}
	}
?>
    
    
    <!--=================Body Contain Start=======================-->
    <style>
    .notification_span
    {
		font-size: 12px;
    	font-weight: normal;
	}
	.visa{
		cursor: pointer;
	}
	</style>
    	<div class="wrapper">
		      <div class="header">
		        <div class="contain-wrap2" style="margin-top:30px; width:948px; ">
		          <form enctype="multipart/form-data" id="image_form" name="image_form" method="post" onsubmit="return checkValidation();">
			          <table width="948" border="0" cellspacing="10px" cellpadding="10px" class="new" style="font-size:16px; font-weight:bold;border-collapse:separate !important;">
			            <tr>
			              <td width="214">Enter Title</td>
			              <td width="514"><input name="blog[blog_title]" id="blog_title" type="text" class="new_text" value="<?php echo $data["blog_title"] ?>"/></td>
			            </tr>
			            
			            <!--<tr>
			              	<td>Image</td>
			              	<td>  
						  		
							  	<input type="file" id="selectedFile" name="selectedFile" style="display: none;" onChange="upload();" accept="image/*"/><a href="javascript:void(0)" onclick="return openFile();"><img <?php if($data["blog_image"]!="") { ?>src="<?php echo "uploads/image_crop.php?image=/uploads/blog_images/".$data["blog_image"]."&width=275&height=162&cropratio=275:162" ?>"<?php } else { ?> src="images/upload.png" <?php } ?> style="border:1px solid #999;border:none" id="blog_display_img" /></a><div id="image_loader_div" style="display:none;"><img src="images/LoadingBar.gif" width="64px" height="64px"/></div>
							  	<input type="hidden" id="blog_image" name="blog[blog_image]" value="<?php echo $data["blog_image"]; ?>" />
							  	
							</td>
			            </tr>-->
			            <tr>
			              <td>Blog Text</td>
			              <td>
						  	<textarea name="blog[blog_description]" id="blog_description" ><?php echo $data["blog_description"] ?></textarea>
						  </td>
			            </tr>
			            <tr>
			              <td>Upload Link</td>
			              <td>
						  		<input type="text" name="blog[link_address]" id="link_address" value="<?php echo $data["link_address"] ?>" />
						  		<br>
						  		<span class="notification_span">* Enter Multiple links separated by comma</span>
						  </td>
			            </tr>
			            <tr>
						  	<td colspan="2" align="center">
							  	<input type="submit" title="Submit" class="visa" value="Submit" style="margin-left:20px" />
							  	<input type="hidden" name="blog[blog_id]" id="blog_id" value="<?php echo $data["blog_id"] ?>" />
							</td>
					  </tr>
			          </table>
		          </form>
		          <div class="clearfix"></div>	  
		        </div>
		        <div class="clearfix"></div>
		      </div>
    	</div>
            
    <!--=================Body Contain end=======================-->
    </div> 
    
    <?php
    require_once("includes/footer.php");
?>
<script>
CKEDITOR.replace( 'blog_description' );
$(function() {
	$('#link_address').tagsInput({width:'auto'});
});
function checkValidation()
{
	if(document.getElementById("blog_title").value.trim()=="")
	{
		alertify.alert("Please fill blog title.");
		document.getElementById("blog_title").focus();
		return false;
	}
	else if(document.getElementById("blog_content").value.trim()=="")
	{
		alertify.alert("Please fill blog text.");
		document.getElementById("blog_content").focus();
		return false;
	}
	return true;
}
<?php
if(isset($msg_success) && $msg_success!="")
{
    ?>
    alertify.alert("<?php echo $msg_success; ?>");
    <?php
}

if(isset($msg_error) && $msg_error!="")
{
    ?>
    alertify.alert("<?php echo $msg_error; ?>");
    <?php
}
?>
</script>
